package cn.com.dcsgo;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;

/**
 * FileNameUtils
 *
 * @author Dcsgo
 * @since 2025/4/20 上午6:06
 */

public class FileUtils {
    public static String removeSuffixName(File file) {
        if (file == null) {
            throw new IllegalArgumentException("文件为null");
        }
        String fileName = file.getName();
        return removeSuffixName(fileName);
    }

    public static String removeSuffixName(String fileName) {
        if (fileName == null) {
            throw new IllegalArgumentException("文件名为null");
        }
        int indexDot = fileName.lastIndexOf(".");
        if (indexDot == -1) {
            return fileName;
        }
        return fileName.substring(0, indexDot);
    }

    public static double calculateFileNameSimilarity(String s1, String s2) {
        String suffix1 = removeSuffixName(s1);
        String suffix2 = removeSuffixName(s2);
        return JaroWinkler.jaroWinkler(suffix1, suffix2);
    }

    public static File[] getSortedFiles(String pathname) {
        File directory = new File(pathname);
        File[] files = directory.listFiles();
        if (files == null) {
            throw new IllegalArgumentException(pathname + " 目录下没有任何文件");
        }
        Arrays.sort(files);
        return files;
    }

    public static HashMap<String, LinkedList<File>> getSimilarityFiles(String pathname) {
        double sim = 0.75;
        File[] files = getSortedFiles(pathname);
        HashMap<String, LinkedList<File>> similarFiles = new HashMap<>();
        if (files.length <= 1) {
            return null;
        }
        //当前文件名与前后文件进行比较
        for (int i = 0; i < files.length; i++) {
            double similarity = 0;
            File preFile = null;
            File nextFile = null;
            if (i > 0) {
                preFile = files[i - 1];
            }
            if (i < files.length - 1) {
                nextFile = files[i + 1];
            }
            LinkedList<File> fileLinkedList = new LinkedList<>();
            if (preFile != null) {
                similarity = calculateFileNameSimilarity(preFile.getName(), files[i].getName());
            }
            if (similarity >= sim) {
                fileLinkedList.add(preFile);
            }
            similarity = 0;
            if (nextFile != null) {
                similarity = calculateFileNameSimilarity(nextFile.getName(), files[i].getName());
            }
            if (similarity >= sim) {
                fileLinkedList.add(nextFile);
            }

            if (!fileLinkedList.isEmpty()) {
                String preSimilarFile = null;
                if (similarFiles.containsKey(files[i].getName())) {
                    System.out.println("当前文件：" + files[i].getName() + " 已经有过重复文件了");
                    preSimilarFile = files[i].getName();
                }
                for (File similarFile : fileLinkedList) {
                    if (similarFiles.containsKey(similarFile.getName())) {
                        System.out.println("当前文件：" + files[i].getName() + " 已经有过重复文件：" + similarFile.getName());
                        preSimilarFile = similarFile.getName();
                        break;
                    }
                }
                if (preSimilarFile == null) {
                    System.out.println("当前文件是首次发现有重复文件的：" + files[i].getName());
                    fileLinkedList.add(files[i]);
                    similarFiles.put(files[i].getName(), fileLinkedList);
                } else {
                    fileLinkedList.add(files[i]);
                    similarFiles.get(preSimilarFile).removeAll(fileLinkedList);
                    similarFiles.get(preSimilarFile).addAll(fileLinkedList);
                }
            }
        }
        return similarFiles;
    }
}
